package com.egghiro.j2se.game.sace.model.actor.bullet.bulletshooter;

import com.egghiro.framework.j2se.game.core.model.actor.IActor2D;
import com.egghiro.j2se.game.sace.device.factory.bulletfactory.BulletFactory;
import com.egghiro.j2se.game.sace.model.actor.BulletCluster;
import com.egghiro.j2se.game.sace.param.SACEConst;

public class AutoBulletShooter extends BulletShooter {
    
    /**
     * 装弹时间
     */
    private final long RELOAD_DELAY;

    private final int RESET_AMMUNITION;
    /**
     * Times can shoot
     */
    protected int ammunition;
    
    private boolean isActing = false;
    
    /**
     * 直线射击
     * @param shiftX
     * @param shiftY
     * @param canvasBulletCluster
     * @param factory
     * @param interval 单发间隔
     * @param ammunition
     * @param reloadDelay (装弹时间)
     */
    public AutoBulletShooter(int shiftX, int shiftY, BulletCluster canvasBulletCluster,
            BulletFactory factory, long interval, int ammunition, long reloadDelay) {
        this(shiftX, shiftY, 0, 0, canvasBulletCluster, factory, interval,ammunition,reloadDelay);
    }

    /**
     * 
     * @param shiftX
     * @param shiftY
     * @param coverAngle
     * @param stepAngle
     * @param canvasBulletCluster
     * @param factory
     * @param interval 单发间隔
     * @param ammunition
     * @param reloadDelay (装弹时间)
     */
    public AutoBulletShooter(int shiftX, int shiftY, int coverAngle, int stepAngle, BulletCluster canvasBulletCluster,
            BulletFactory factory, long interval, int ammunition, long reloadDelay) {
        super(shiftX, shiftY, coverAngle, stepAngle, canvasBulletCluster, factory, interval);
        // TODO 默认弹药数小于0的时候怎么办 默认设为1000之类的？
        this.RESET_AMMUNITION = ammunition;
        this.RELOAD_DELAY = reloadDelay;
        this.ammunition = this.RESET_AMMUNITION;
    }
    
    /**
     *
     * @param flyerX 子弹的RefX
     * @param flyerY 子弹的RefY
     * @param target
     * @param axisAngle 指定射击的方向(中心线)
     */
    @Override
    public final void act(int flyerX, int flyerY, IActor2D target, int axisAngle) {
        if (isActing) {
            if (delay > 0) {
                delay -= SACEConst.SLEEP;
            } else {
                fire(flyerX, flyerY, target, axisAngle);
                if (this.ammunition <=0) {
                    reload();
                } else {
                    nextFireDelay();    
                }
//                if (ammunition <=0) {// TODO 虽然不太记得当时的想法，但是大概是想做一个只有一梭子子弹打完就停的发射器。具体怎么做以后再想。。。
//                    this.stop();
//                }
            }
        }
    }
    
    /**
     * 默认实现(不指定速度)<br>
     * 没有延迟直接发射。若需要延迟，请调用act（）
     * @param flyerX 本体的RefX
     * @param flyerY 本体的RefY
     * @param target
     * @param axisAngle 指定射击的方向(中心线)
     */
    @Override
    protected void fire(int flyerX, int flyerY, IActor2D target, int axisAngle){
        super.fire(flyerX, flyerY, target, axisAngle);
        this.ammunition--;
    }

    public void reload(){
        this.isActing = true;
        this.delay = RELOAD_DELAY;
        this.ammunition = RESET_AMMUNITION;
    }

    public void start(){
        this.isActing = true;
        this.delay = 0;
        this.ammunition = RESET_AMMUNITION;
    }

    public void stop(){
        isActing = false;
    }
}
